package cs.third;

import java.util.concurrent.Callable;

class CouponTask implements Callable<Boolean> {
    private final Customer customer;
    private final Coupon coupon;

    public CouponTask(Customer customer, Coupon coupon) {
        this.customer = customer;
        this.coupon = coupon;
    }

    @Override
    public Boolean call() {
        synchronized (this) {
            // 模拟网络延迟
            try {
                Thread.sleep((long) (Math.random() * 100));
            } catch (InterruptedException e) {
                return false;
            }
        }

        boolean success = coupon.trySeize();
        System.out.printf("%s 抢购%s%n",
                customer.getName(),
                success ? "成功！剩余：" + coupon.getRemaining() : "失败！优惠券已抢完");
        return success;
    }
}

